using System;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Drawing;

namespace Camalon.ColorPicker.Design
{
	public class RoundedPercentConverter:PercentConverter
	{
		public RoundedPercentConverter():base( 0 ){}
	}

	
	/// <summary>
	/// Summary description for TransparencyConverter.
	/// </summary>
	public class PercentConverter:TypeConverter
	{
		private int _decimals = 1;

		public PercentConverter(int decimals)
		{
			_decimals = decimals;
		}

		public PercentConverter()
		{

		}
		public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
		{
			if (destinationType == typeof(string))	return true;
			if (destinationType == typeof(float))	return true;
			if (destinationType == typeof(double))	return true;
			if (destinationType == typeof(decimal)) return true;

			return base.CanConvertTo (context, destinationType);
		}

		public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
		{
			if (sourceType == typeof(string)) return true;
			return base.CanConvertFrom (context, sourceType);
		}

		public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
		{
			if ( value is string ) 
			{
				string sVal = (string)value;
				sVal = sVal.Trim();
				if( sVal.Length == 0) throw new InvalidOperationException("Input string was not in a correct format");

				string[] args = sVal.Split(culture.NumberFormat.PercentSymbol.ToCharArray());

				if( args.Length == 0) throw new InvalidOperationException("Input string was not in a correct format");
				sVal = args[0];

				if( context.PropertyDescriptor.PropertyType == typeof(double))
				{
					double val = double.Parse( sVal);
					//					val = System.Math.Round( val, 1);
					return val/100d;
				}
				
				if( context.PropertyDescriptor.PropertyType == typeof(float))
				{
					float val = float.Parse( sVal);
					//					val = (float)System.Math.Round( val, 1);
					return val/100f ;
				}

				if( context.PropertyDescriptor.PropertyType == typeof(decimal))
				{

					decimal val = decimal.Parse( sVal);
					//					val = System.Math.Round( val, 1);
					return val/100m;
				}

			}
			

			return base.ConvertFrom (context, culture, value);
		}


		public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
		{
			if (destinationType == typeof(string)) 
			{
				if( value is double)
				{
					double val = (double)value * 100d; 
					val = System.Math.Round( val, _decimals);
					return val.ToString()+culture.NumberFormat.PercentSymbol;
				}
				
				if( value is float)
				{
					float val = (float)value * 100f; 
					val = (float)System.Math.Round( val, _decimals);
					return val.ToString()+culture.NumberFormat.PercentSymbol;
				}

				if( value is decimal)
				{
					decimal val = (decimal)value * 100m; 
					val = System.Math.Round( val, _decimals);
					return val.ToString()+culture.NumberFormat.PercentSymbol;
				}


			}


			
			return base.ConvertTo (context, culture, value, destinationType);
		}

	}

}
